package com.wss.merchant.controller;


import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.auth0.jwt.interfaces.Claim;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.baomidou.mybatisplus.core.conditions.AbstractWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wss.merchant.domain.CommonResult;
import com.wss.merchant.domain.DlNewCommunity;
import com.wss.merchant.domain.DlNewCommunityManager;
import com.wss.merchant.mapper.DlNewCommunityManagerMapper;
import com.wss.merchant.service.DlNewCommunityManagerService;
import com.wss.merchant.service.DlNewCommunityService;
import com.wss.merchant.util.JWTUtil;
import jdk.nashorn.internal.ir.RuntimeNode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  社区管理员表
 *  前端控制器
 * </p>
 *
 * @author wss
 * @since 2021-02-25
 */
@Slf4j
@Controller
@RequestMapping("/manager")

public class DlNewCommunityManagerController {
    @Autowired
    private DlNewCommunityManagerService dlNewCommunityManagerService;
    @Autowired
    private DlNewCommunityService dlNewCommunityService;
    @Autowired
    private JWTUtil jwtUtil;

    //注册用户
    @RequestMapping("/register")
    @ResponseBody
    public CommonResult register(@RequestBody DlNewCommunityManager dlNewCommunityManager){
            QueryWrapper wrapper = new QueryWrapper();
            wrapper.eq("name", dlNewCommunityManager.getName());
            DlNewCommunityManager one = dlNewCommunityManagerService.getOne(wrapper);
            if(one == null){
//                log.info(dlNewCommunityManager.toString());
//                long time = new Date().getTime();
//                dlNewCommunityManager.setAddtime(time);
                //密码加密
                String password = dlNewCommunityManager.getPassword();
                password= DigestUtils.md5DigestAsHex(password.getBytes());
                dlNewCommunityManager.setPassword(password);
                log.info(dlNewCommunityManager.toString());
                dlNewCommunityManagerService.save(dlNewCommunityManager);
                return new CommonResult(200,"注册成功 请登入");
            }else {
                return new CommonResult(200,"用户名已存在");
            }
    }
    //登入用户
    @RequestMapping("/login")
    @ResponseBody
    public CommonResult login(@RequestBody DlNewCommunityManager dlNewCommunityManager){

        QueryWrapper wrapper = new QueryWrapper();

        wrapper.eq("name", dlNewCommunityManager.getName());
//        try {
            DlNewCommunityManager one = dlNewCommunityManagerService.getOne(wrapper);
            String password1 = one.getPassword();
            String password2 = dlNewCommunityManager.getPassword();
            password2= DigestUtils.md5DigestAsHex(password2.getBytes());
            if(password1.equals(password2)){
                //生成token
                String token = jwtUtil.getToken(one.getId(), one.getName());
                HashMap<String, String> hashMap = new HashMap<>();
                hashMap.put("token",token);
                hashMap.put("name",one.getName());
                CommonResult<Map> commonResult = new CommonResult<>(200, "登入成功", hashMap);
                return commonResult;
            }else {
                CommonResult commonResult = new CommonResult<>(200, "账号或密码出错");
                return commonResult;
            }
//        }catch (Exception e){
//            return new  CommonResult(200,"用户不存在");
//        }
    }
    //修改密码

    //获取当前社区
    @RequestMapping("/getCommunity")
    @ResponseBody
    public CommonResult getCommunity(HttpServletRequest request){

        String token = request.getHeader("token");
        Integer userId = jwtUtil.verifyToken(token).getClaim("userId").asInt();
        Integer cid = dlNewCommunityManagerService.getById(userId).getCid();
        if(cid == null){
            return new CommonResult(200,"点击绑定社区");
        }else {
            DlNewCommunity byId = dlNewCommunityService.getById(cid);
            if(byId == null){
                return new CommonResult(200,"点击绑定社区");
            }else {
                String name = byId.getName();
                return new CommonResult(200,name);
            }
        }
    }

    //绑定社区
    @RequestMapping("/setCommunity")
    @ResponseBody
    private String setCommunity(HttpServletRequest request ,@RequestBody Map<String,String> map){
        String token = request.getHeader("token");
        Integer userId = jwtUtil.verifyToken(token).getClaim("userId").asInt();
        DlNewCommunityManager blNewCommunityManager = dlNewCommunityManagerService.getById(userId);
        String communityName = map.get("communityName");
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("name",communityName);
        DlNewCommunity one = dlNewCommunityService.getOne(queryWrapper);
        if(one == null){
            return "没有当前社区";
        }else {
            Integer id = one.getId();
            blNewCommunityManager.setCid(id);
            dlNewCommunityManagerService.saveOrUpdate(blNewCommunityManager);
            return "绑定社区成功";
        }

    }


}

